package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.CategoryMapper;
import com.dmpp.mes.dao.SpecificationsMapper;
import com.dmpp.mes.parameterInfo.Category;
import com.dmpp.mes.parameterInfo.Specifications;
import com.dmpp.mes.service.CategoryService;
import com.dmpp.mes.service.SpecificationsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "SpecificationsService")

public class SpecificationsServiceImpl implements SpecificationsService {

    @Autowired
    private SpecificationsMapper mapper;

    @Override
    public ResponseData findAll(Specifications specifications) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(specifications));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(Specifications specifications) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(specifications));
        return responseData;
    }

    @Override
    public ResponseData insert(Specifications specifications) {
        ResponseData responseData = new ResponseData();
        Specifications specifications1 = mapper.findByCode(specifications);
        if (specifications1 == null) {
            mapper.insert(specifications);
        } else {
            responseData.setCodeEnum(RespEnum.ERROR2001);
        }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(Specifications specifications) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(specifications);
        return responseData;
    }


    @Override
    public ResponseData deleteByCode(Specifications specifications) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(specifications);
        return responseData;
    }

    @Override
    public ResponseData delete(Specifications specifications) {
        ResponseData responseData = new ResponseData();
        Integer count = mapper.count(specifications);
        Integer semiProductCount = mapper.semiProductCount(specifications);

        if (specifications.getMakingsState().equals("原材料")){
            if (count.equals(0)){
                mapper.delete(specifications);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1016);
            }
        }
        if (specifications.getMakingsState().equals("半成品")){
            if (semiProductCount.equals(0)){
                mapper.delete(specifications);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1018);
            }
        }
        return responseData;
    }

}
